// packageE/appointment/client/project/project.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    qrCodeFlag: false,
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    posterImageUrl: "",
    projectList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  changeTags(e) {
    console.log(e.detail.index, this.data.active);
    this.data.active = e.detail.index;
    this.getListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getListData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },
  _getMoreData() {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let urlStr = app.getNetAddresss(
        "plugin.appointment.frontend.appointment-order.get-list"
      );
      this.data.page++;
      app._postNetWork({
        url: urlStr,
        data: {
          status: this.data.active,
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result != 1) return;
          if (res.data.current_page >= res.data.last_page) {
            that.setData({
              isLoadMore: false,
            });
          }
          let projectList = this.data.projectList;
          projectList.push(...res.data.data);
          this.setData({ projectList });
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  setQRCodeFlag() {
    this.setData({
      qrCodeFlag: !this.data.qrCodeFlag,
    });
  },
  createQrc(evt) {
    wx.showLoading({
      title: "生成中",
      mask: true,
    });
    let item = evt.currentTarget.dataset.item;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.appointment-order.qr-code-url"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: item.id,
        tag: "pro",
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);

        this.setData({
          qrCodeFlag: true,
          posterImageUrl: res.data.miQrCodeUrl,
        });
      },
      fial: (err) => {
        wx.hideLoading();
      },
    });
  },
  toAppointment(evt) {
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url:
        "/packageE/appointment/client/make_appointment/make_appointment?id=" +
        item.id +
        "&project_id=" +
        item.project_id,
    });
  },
  initData() {
    this.data.page = 1;
    this.data.isLoadMore = true;
    this.data.total_page = 0;
  },
  getListData() {
    this.initData();
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.appointment-order.get-list"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: 1,
        status: this.data.active,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);

        this.data.isLoadMore = true;
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          projectList: res.data.data,
        });
      },
    });
  },
  tips(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 2000,
    });
    return false;
  },

  goProjectDetails(e){
    let id = e.currentTarget.dataset.id;
    let point = JSON.stringify(wx.getStorageSync('address-point').point);
    wx.navigateTo({
      url: '/packageE/appointment/ProjectDetails/ProjectDetails?project_id=' + id + '&point=' + point,
    });
  },
});
